package com.cms.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mysql.fabric.Response;


/**
*@author:gang
*@version:
**/
@Controller
@RequestMapping("/test")
public class MVCTest {
	
	private Map<String, String>	map;
	private List<String> list;
	private String [] array ={"1","2","3"};
	private String name = new String("110");
	
	@RequestMapping("/modelandview")
	public ModelAndView modelAndViewTest(HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView();
		//设置对比参数
		//记住，要初始化，不然会包NullpointException
		map = new HashMap<String, String>();
		HashMap<String, String> map2 = new HashMap<String, String>();
		map2.put("mapkey1", "001");
		map2.put("mapkey3", "002");
		map.put("mapkey", "mapvalue1_001");
		map.put("mapkey1", "mapvalue1_002");
		list = new LinkedList<String>();
		list.add("list1_1");
		list.add("list1_2");
		array[0]="1_1";
		name="modelandview";
		String name2= "112";
//		modelAndView.addAllObjects(modelMap);

		modelAndView.addObject(map);
		modelAndView.addObject(map2);
//		modelAndView.addObject(name);
//		modelAndView.addAllObjects(modelMap);
		modelAndView.addObject("map", map);	
		modelAndView.addObject("list", list);	
		modelAndView.addObject("array", array);
		modelAndView.addObject("name", name);
		HttpSession session = request.getSession();
		session.setAttribute("name2", name2);
		System.out.println(modelAndView.isEmpty());
		modelAndView.setViewName("mvctest");
		System.out.println(modelAndView.getViewName());
		
		
//		//sessionTest
//		System.out.println("java"+request.getSession());
////		
//		View view = (View) new ViewControllerRegistration("/WEB-INF/static/cms/mvctest.jsp");
//		modelAndView.setView(view);
//		System.out.println(modelAndView.getView());
		return modelAndView;
	}
//	
	@RequestMapping("/modelmap")
	public String modelMap(ModelMap modelMap){
		//设置对比参数
		map.put("mapkey", "mapvalue2_001");
		map.put("mapkey1", "mapvalue2_002");
		list.add("list2_1");
		list.add("list2_2");
		array[0]="2_1";
		name="modelMAP";
		modelMap.put("map", map);
		modelMap.put("name", name);
		modelMap.addAttribute("list", list);
		modelMap.put("array", array);
		return "mvctest";
	} 
//	
	@RequestMapping("/model")
	public String modelTest(Model model){
		//设置对比参数
		map.put("mapkey", "mapvalue3_001");
		map.put("mapkey1", "mapvalue3_002");
		list.add("list3_1");
		list.add("list3_2");
		array[0]="3_1";
		name="model";
		model.addAttribute("name", name);
		model.addAttribute("list", list);
		model.addAttribute("array",array);
		model.addAttribute(map);
		return "mvctest";
	}
	
	@RequestMapping("/map")
	public String mapTest(Map<String, Object> map){
		map.put("mapkey", "mapvalue3_001");
		map.put("mapkey1", "mapvalue3_002");
		list.add("list3_1");
		list.add("list3_2");
		name="map";
		map.put("name", name);
		map.put("map", map);
		map.put("list", list);
        map.put("array", Arrays.asList("map4_1", "map4_2", "map4_3"));
		return "mvctest";
	}
	
	@ModelAttribute(value="modelAttribute")
	public String modelAttribute(){
		return "modelAttribute success";
	}
	
	@ModelAttribute
	public void modelAttribute(ModelMap model){
		model.addAttribute("modelAttribute02", "modelAttribute02");
	}
	
	
	
}
